<?php

namespace App\Http\Controllers\Admin;

use App\Model\M9_cate;
use App\Model\M9_good;
use App\Services\OSS;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;

class GoodController extends Controller
{
    /**
     * 商品列表页
     */
    public function index(Request $request)
    {
//       // $count = $request -> input('count',2);
        $count = $request -> input('count',5);
       $search = $request -> input('search','');
      $gcid = $request->input('gcid','');
      if($search==null &&$gcid==null){
          $data = M9_good::where('gname','like','%'.$search.'%')->paginate($count);
          $request = $request -> all();
          //dd($data);
      }elseif ($search==null && $gcid!==null){
          $data = M9_good::where('gname','like','%'.$search.'%')->where('g_cid',$gcid)->paginate($count);
          $request = $request -> all();
      }elseif($search!==null && $gcid==null){
          $data = M9_good::where('gname','like','%'.$search.'%')->paginate($count);
          $request = $request -> all();
      }else{
          $data = M9_good::where('gname','like','%'.$search.'%')->where('g_cid',$gcid)->paginate($count);
          $request = $request -> all();
      }

        // 查询数据并分页
//       $data = M9_good::where('gname','like','%'.$search.'%')->where('g_cid',$gcid)->paginate(5);
//        $request = $request -> all();
//        dd($data);



        //替换商品表里的类名
        if($data !==null){
            foreach($data as $k=>$v){
                $info = M9_good::find($v['gid'])->cate->cname;
                $data[$k]['cname'] = $info;
            }
        }


        //$data = M9_good::find(1)->cate()->first();
        //dd($data);
        $s = self::getCates();
        return view('admin.good.index',['data'=>$data ,'s'=>$s,'request'=>$request]);

    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $s = self::getCates();

        return view('admin.good.create',['s'=>$s]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
        $input = $request->except('_token');

//        //实例化文件上传
//        $file = Input::file('gphoto');
//        if($file->isValid()){
//            $entension = $file->getClientOriginalExtension();//上传文件的后缀名
//            $newName = date('YmdHis').mt_rand(1000,9999).'.'.$entension;
//
//            $path = $file->move(public_path().'/goodphoto',$newName);
//            $filepath = 'uploads/'.$newName;
//        $input['gphoto'] = $filepath;
        //
        //
        //
        //存到阿里云
        // 获取表单提交的图片
        $pic = $request->file('gphoto');
        // 判断图片有效性
        if (!$pic->isValid()) {
            return back()->with('error','图片无效');
        }
        $entension = $pic->getClientOriginalExtension();//上传文件的后缀名
        $newName = date('YmdHis').mt_rand(1000,9999).'.'.$entension;

        // 获取图片在临时文件中的地址
        $pic = $pic->getRealPath();
        //阿里 OSS 图片上传
        $result = OSS::upload("goodphoto/".$newName, $pic);

        if($result){
            $filepath = 'goodphoto/'.$newName;
            $input['gphoto'] = $filepath;

            //添加
            // $m9_good = new M9_good;
            $res = M9_good::create($input);

            if($res){
                return redirect('/admin/good')->with('success','添加成功');
            }else{
                return back()->with('error','添加失败');
            }
        }else{
            return back()->with('error','图片没有上传成功');
        }



    }


    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $s = self::getCates();
       $data = M9_good::find($id)->all();

        return view('/admin/good/edit',['data'=>$data[0],'s'=>$s]);
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
        $input = $request->except('_token','_method');

        //存到阿里云
        // 获取表单提交的图片
        $pic = $request->file('gphoto');
        // 判断图片有效性
        if (!$pic->isValid()) {
            return back()->with('error','图片无效');
        }
        $entension = $pic->getClientOriginalExtension();//上传文件的后缀名
        $newName = date('YmdHis').mt_rand(1000,9999).'.'.$entension;

        // 获取图片在临时文件中的地址
        $pic = $pic->getRealPath();
        //阿里 OSS 图片上传
        $result = OSS::upload("goodphoto/".$newName, $pic);

        if($result){
            $filepath = 'goodphoto/'.$newName;
            $input['gphoto'] = $filepath;


            //添加
            // $m9_good = new M9_good;
            $res = M9_good::where('gid',$id)->update($input);

            if($res){
                return redirect('/admin/good')->with('success','添加成功');
            }else{
                return back()->with('error','添加失败');
            }
        }else{
            return back()->with('error','图片没有上传成功');
        }


    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function delete($id)
    {
        //
      $res = M9_good::destroy($id);
        if($res){
            return redirect('/admin/good')->with('success','添加成功');
        }else{
            return back()->with('error','添加失败');
        }
    }

    /**
     * 添加字段
     */
    static public function getCates(){
        $data = DB::select(" select * ,concat(cpath,',',cid)as paths from m9_cate order by  paths");
        foreach($data as $key => $v){
            $len = substr_count($v['cpath'],',');
            //重复使用一个字符串
            $data[$key]['cname'] = str_repeat('|----',$len).$v['cname'];
        }
        return $data;
    }

    public function upload()
    {


//      $  $file = Input::file('file_upload');
//        if($file->isValid()){
//            $entension = $file->getClientOriginalExtension();//上传文件的后缀名
//            $newName = date('YmdHis').mt_rand(1000,9999).'.'.$entension;
//            $path = $file->move(public_path().'/uploads',$newName);
//            $filepath = 'uploads/'.newName;
//            return  $filepath;
//        }
    }
}
